{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 二叉树的层序遍历\n",
    "\n",
    "![Asked by Microsoft](https://img.shields.io/badge/Asked%20by-Microsoft-important?style=for-the-badge&logo=microsoft)\n",
    "\n",
    "&emsp;&emsp;给定一棵二叉树，以层序遍历方式输出。例如：\n",
    "\n",
    "```text\n",
    "#   1\n",
    "#  / \\\n",
    "# 2   3\n",
    "#    / \\\n",
    "#   4   5\n",
    "```\n",
    "\n",
    "&emsp;&emsp;正确的输出应该是 `1, 2, 3, 4, 5`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def printLevelOrder(node: dict) -> str:\n",
    "    if not node:\n",
    "        return None\n",
    "    queue = [node]\n",
    "    result = ''\n",
    "    while len(queue):\n",
    "        current = queue.pop(0)\n",
    "        result += str(current['value'])\n",
    "        if 'left' in current.keys():\n",
    "            queue.append(current['left'])\n",
    "        if 'right' in current.keys():\n",
    "            queue.append(current['right'])\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "node = {'value': 1}\n",
    "node['left'] = {'value': 2}\n",
    "node['right'] = {'value': 3}\n",
    "node['right']['left'] = {'value': 4}\n",
    "node['right']['right'] = {'value': 5}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12345\n"
     ]
    }
   ],
   "source": [
    "print(printLevelOrder(node))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
